<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>地形曲率计算</title>
    <style type="text/css">
        body{
            margin: 0;
            overflow: hidden;
            background: #fff;
        }
        #map{
            position: relative;
            height: 510px;
            border:1px solid #3473b7;
        }
        #toolbar {
            position: relative;
            padding-top: 5px;
            padding-bottom: 10px;
        }
    </style>
    <link href='./css/bootstrap.min.css' rel='stylesheet' />
    <link href='./css/bootstrap-responsive.min.css' rel='stylesheet' />
    <script src='../libs/SuperMap.Include.js'></script>
    <script type="text/javascript">
        var map, baseLayer, layersID, resultLayer,
                host = document.location.toString().match(/file:\/\//)?"http://localhost:8090":'http://' + document.location.host,
                url=host+"/iserver/services/map-jingjin/rest/maps/京津地区地图",
                url2=host+"/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";
        function init(){
            map = new SuperMap.Map("map",{controls: [
                new SuperMap.Control.LayerSwitcher(),
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
            });
            baseLayer = new SuperMap.Layer.TiledDynamicRESTLayer("京津地区地图", url, {transparent: true, cacheEnabled: true}, {maxResolution: "auto"});
            baseLayer.events.on({"layerInitialized": addLayer});
        }

        function addLayer() {
            map.addLayer(baseLayer);
            map.setCenter(new SuperMap.LonLat(117.2, 40.11), 0);
            map.allOverlays = true;
        }
        function addResultLayer() {
            removeTheme();

            //创建地形曲率计算服务实例
            var terrainCurvatureCalculationService = new SuperMap.REST.TerrainCurvatureCalculationService(url2, {eventListeners:{"processCompleted": terrainCurvatureCalculationCompleted, "processFailed": terrainCurvatureCalculationFailed}});

            //创建地形曲率计算参数实例
            var terrainCurvatureCalculationParameters = new SuperMap.REST.TerrainCurvatureCalculationParameters({
                dataset: "JingjinTerrain@Jingjin",
                zFactor: 1.0,
                averageCurvatureName: "CurvatureA",
                deleteExistResultDataset: true
            });

            //向 iServer 发起地形曲率计算请求
            terrainCurvatureCalculationService.processAsync(terrainCurvatureCalculationParameters);
        }

        //用栅格专题图展示分析结果
        function terrainCurvatureCalculationCompleted(terrainCurvatureCalculation) {
            var color1 = new SuperMap.REST.ServerColor(0,0,255),
                    color2 = new SuperMap.REST.ServerColor(0,0,125),
                    color3 = new SuperMap.REST.ServerColor(0,125,125),
                    color4 =  new SuperMap.REST.ServerColor(0,125,0),
                    color5 = new SuperMap.REST.ServerColor(0,255,0),

                    themeGridRangeIteme1 = new SuperMap.REST.ThemeGridRangeItem({
                        start: -9000000,
                        end: -1000,
                        color: color1
                    }),
                    themeGridRangeIteme2 = new SuperMap.REST.ThemeGridRangeItem({
                        start: -1000,
                        end:  -1,
                        color: color2
                    }),
                    themeGridRangeIteme3 = new SuperMap.REST.ThemeGridRangeItem({
                        start: -1,
                        end:  1,
                        color: color3
                    }),
                    themeGridRangeIteme4 = new SuperMap.REST.ThemeGridRangeItem({
                        start: 1,
                        end:  1000,
                        color: color4
                    }),
                    themeGridRangeIteme5 = new SuperMap.REST.ThemeGridRangeItem({
                        start: 1000,
                        end:  20000000,
                        color: color5
                    }),

                    themeGridRange = new SuperMap.REST.ThemeGridRange({
                        reverseColor:false,
                        rangeMode: SuperMap.REST.RangeMode.EQUALINTERVAL,
                        //栅格分段专题图子项数组
                        items: [themeGridRangeIteme1,
                            themeGridRangeIteme2,
                            themeGridRangeIteme3,
                            themeGridRangeIteme4,
                            themeGridRangeIteme5
                        ]
                    }),
                    themeParameters = new SuperMap.REST.ThemeParameters({
                        //制作专题图的数据集（地形曲率计算的结果数据集）
                        datasetNames:[terrainCurvatureCalculation.result.averageCurvatureResult.dataset.split('@')[0]],
                        dataSourceNames: ["Jingjin"],
                        joinItems: null,
                        themes: [themeGridRange]
                    });
            var themeService = new SuperMap.REST.ThemeService(url, {eventListeners:{"processCompleted": themeCompleted, "processFailed": themeFailed}});
            themeService.processAsync(themeParameters);
        }
        function themeCompleted(themeEventArgs) {
            if(themeEventArgs.result.resourceInfo.id) {
                resultLayer = new SuperMap.Layer.TiledDynamicRESTLayer("地形曲率计算结果", url, {cacheEnabled:false,transparent: true,layersID: themeEventArgs.result.resourceInfo.id}, {"maxResolution":"auto"});
                resultLayer.events.on({"layerInitialized":addThemelayer});
            }
        }
        function addThemelayer() {
            //将专题图添加到地图
            map.addLayer(resultLayer);
        }
        function themeFailed(serviceFailedEventArgs) {
            alert(serviceFailedEventArgs.error.errorMsg);
        }
        function terrainCurvatureCalculationFailed(serviceFailedEventArgs) {
            alert(serviceFailedEventArgs.error.errorMsg);
        }

        //移除专题图
        function removeTheme() {
            if (map.layers.length > 1) {
                map.removeLayer(resultLayer, true);
            }
        }
    </script>
</head>
<body onload="init()">
<div id="toolbar">
    <input type="button" class="btn" value="地形曲率计算" onclick="addResultLayer()" />
    <input type="button" class="btn" value="移除结果图层" onclick="removeTheme()" />
</div>
<div id="map"></div>
</body>
</html>
